package com.niannian.activity;

import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.niannian.util.AsyncHandle;
import com.niannian.util.Common;
import com.niannian.util.ErrorCode;
import com.niannian.util.MyTextWatcher;
import com.niannian.web.nnSyncApi;
import com.niannian.BaseActivity;
import com.niannian.R;

/**
 * 
 * 反馈
 * 
 * @author shengl
 * 
 */
public class FeedbackActivity extends BaseActivity {
	private EditText et_feedback_content;
	private Button bt_feedback_submit;
	String content = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);
		initView();
		setLister();
		initTopView();

	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("反馈");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);

		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myfinish();
			}
		});
	}

	private void initView() {
		et_feedback_content = (EditText) findViewById(R.id.et_feedback_content);

		bt_feedback_submit = (Button) findViewById(R.id.bt_feedback_submit);
		et_feedback_content.addTextChangedListener(textWatcher);
	}

	private void setLister() {

		bt_feedback_submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				content = et_feedback_content.getText().toString().trim();
				if (content.length() < 5) {
					Common.tip(FeedbackActivity.this, "反馈信息不能少于5个字");
				} else {
					new SubmitFeedback().init(FeedbackActivity.this, null, true, "提交中...").execute();
				}
			}
		});
	}

	private MyTextWatcher textWatcher = new MyTextWatcher() {

		@Override
		public void onFocusChange(View view, boolean hasFocus) {
		};

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			if (!Common.empty(et_feedback_content.getText().toString().trim())) {
			//	bt_feedback_submit.setEnabled(true);
			} else {
			//	bt_feedback_submit.setEnabled(false);
			}
		}
	};

	protected class SubmitFeedback extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {
			Common.tip(FeedbackActivity.this, "提交反馈意见失败");
		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {
			int retCode = paramJSONObject.getInt("code");
			if (retCode == ErrorCode.OK) {
				Common.tip(FeedbackActivity.this, "提交反馈意见成功");
				myfinish();
			} else {
				Common.tip(FeedbackActivity.this, ErrorCode.GetErrorMsg(retCode));
			}
		}

		protected String runTask(Map<String, Object> paramMap) {
			return nnSyncApi.feedbacks(content);
		}
	}
}
